<?php
	/**
	 * Javascript tree menu generator
	 *
	 * @package Javascript
	 * @author Perkin Anton
	 * @version 1.1
	 */
	class TreeMenu
	{
		/**
		 * String representation of the menu (for the JS function)
		 *
		 * @var string
		 */
		static $menu = '';

		/**
		 * String representation of the generated menu
		 *
		 * @var string
		 */
		static $result = '';
		
		/**
		 * Creates Javascript multi level menu
		 *
		 * @param array $nodes menu nodes (should have 'name', 'url' (optional string), 'sub' (optional array))
		 */
		static function CreateMenu($nodes, array $image_collection = array())
		{
			//echo '<script language="JavaScript" type="text/javascript" src="/spherus_framework/core/classes/TreeMenu/tree.js"></script>';
			self::$result = '';
			
			self::CreateTemplate($image_collection);
			self::CreateNodes($nodes);
			self::DisplayMenu();
			
			//makes the menu empty
			self::$menu = '';
			
			return self::$result;
		}
		
		/**
		 * Creates menu template (images and styles)
		 *
		 */
		static function CreateTemplate(array $image_collection = array())
		{
			$node_icon = isset($image_collection['node']) ? $image_collection[node] : 'folder.gif';
			$node_open_icon = isset($image_collection['node_open']) ? $image_collection['node_open'] : 'folderopen.gif';
			
			$leaf_icon = isset($image_collection['leaf']) ? $image_collection['leaf'] : 'page.gif';
			
			self::$result .= 
			"<script>
				 var image_dir = '/spherus_framework/core/classes/TreeMenu/';
				 var tree_tpl = {
					'target'  : '_self',	// name of the frame links will be opened in
											// other possible values are: _blank, _parent, _search, _self and _top
				
					'icon_e'  : image_dir + 'icons/empty.gif', // empty image
					'icon_l'  : image_dir + 'icons/line.gif',  // vertical line
				
				        'icon_32' : image_dir + 'icons/base.gif',   // root leaf icon normal
				        'icon_36' : image_dir + 'icons/base.gif',   // root leaf icon selected
				
					'icon_48' : image_dir + 'icons/base.gif',   // root icon normal
					'icon_52' : image_dir + 'icons/base.gif',   // root icon selected
					'icon_56' : image_dir + 'icons/base.gif',   // root icon opened
					'icon_60' : image_dir + 'icons/base.gif',   // root icon selected
					
					'icon_16' : image_dir + 'icons/$node_icon', // node icon normal
					'icon_20' : image_dir + 'icons/$node_open_icon', // node icon selected
					'icon_24' : image_dir + 'icons/$node_open_icon', // node icon opened
					'icon_28' : image_dir + 'icons/$node_open_icon', // node icon selected opened
				
					'icon_0'  : image_dir + 'icons/$leaf_icon', // leaf icon normal
					'icon_4'  : image_dir + 'icons/$leaf_icon', // leaf icon selected
					
					'icon_2'  : image_dir + 'icons/joinbottom.gif', // junction for leaf
					'icon_3'  : image_dir + 'icons/join.gif',       // junction for last leaf
					'icon_18' : image_dir + 'icons/plusbottom.gif', // junction for closed node
					'icon_19' : image_dir + 'icons/plus.gif',       // junctioin for last closed node
					'icon_26' : image_dir + 'icons/minusbottom.gif',// junction for opened node
					'icon_27' : image_dir + 'icons/minus.gif'       // junctioin for last opended node
				};			
			</script>";
		}
		
		/**
		 * Creates nodes' heirarchy
		 *
		 * @param array $nodes menu nodes
		 */
		static function CreateNodes($nodes)
		{
			foreach ($nodes as $node)
			{
				self::$menu .= "['$node[name]', '$node[url]',\n";
				if (!empty($node[sub]))
				{
					self::CreateNodes($node['sub']);
				}
					self::$menu .= '],';
			}			
		}
	
		/**
		 * Displays the tree menu
		 *
		 */
		static function DisplayMenu()
		{
			self::$result .= '
			<script>
				var TREE_ITEMS = ['.self::$menu.'];
			</script>
			
			<script language="JavaScript">
				new tree (TREE_ITEMS, tree_tpl);
			</script>';			
		}
	}
?>